<template>
  <div v-if="show" class="modal-overlay">
    <div class="modal-content">
      <h3>下载库存数据</h3>
      <div class="download-options">
        <div class="download-option" @click="downloadInventory">
          <div class="option-icon">
            <i class="fa fa-file-excel-o"></i>
          </div>
          <div class="option-info">
            <h4>库存可用存量表</h4>
            <p>下载当前实验室所有库存物品的可用存量数据</p>
          </div>
        </div>
        
        <div class="download-option" @click="downloadRecord">
          <div class="option-icon">
            <i class="fa fa-history"></i>
          </div>
          <div class="option-info">
            <h4>库存记录操作表</h4>
            <p>下载库存出入库操作记录数据</p>
          </div>
        </div>
      </div>
      
      <div class="modal-actions">
        <button class="cancel-btn" @click="handleClose">取消</button>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue';
import { API_CONFIG } from '../../config/apiConfig';
import { ToastService } from '../../utils/toastService';

const props = defineProps({
  show: Boolean,
  labId: String
});

const emit = defineEmits(['close']);

const isLoading = ref(false);

const handleClose = () => {
  emit('close');
};

// 下载库存可用存量表
const downloadInventory = async () => {
  if (!props.labId) {
    ToastService.error('实验室ID不能为空');
    return;
  }
  
  try {
    isLoading.value = true;
    
    // 创建一个隐藏的a标签用于下载
    const link = document.createElement('a');
    link.href = `${API_CONFIG.BASE_URL}/inventory/${props.labId}/download`;
    link.target = '_blank';
    link.download = `库存可用存量表_${new Date().toLocaleDateString()}.xlsx`;
    
    // 添加到文档中并触发点击
    document.body.appendChild(link);
    link.click();
    
    // 清理DOM
    document.body.removeChild(link);
    
    // 关闭模态框
    setTimeout(() => {
      handleClose();
    }, 500);
  } catch (error) {
    console.error('下载库存可用存量表失败:', error);
    ToastService.error('下载失败，请检查网络连接');
  } finally {
    isLoading.value = false;
  }
};

// 下载库存记录操作表
const downloadRecord = async () => {
  if (!props.labId) {
    ToastService.error('实验室ID不能为空');
    return;
  }
  
  try {
    isLoading.value = true;
    
    // 创建一个隐藏的a标签用于下载
    const link = document.createElement('a');
    link.href = `${API_CONFIG.BASE_URL}/inventory/${props.labId}/record/download`;
    link.target = '_blank';
    link.download = `库存记录操作表_${new Date().toLocaleDateString()}.xlsx`;
    
    // 添加到文档中并触发点击
    document.body.appendChild(link);
    link.click();
    
    // 清理DOM
    document.body.removeChild(link);
    
    // 关闭模态框
    setTimeout(() => {
      handleClose();
    }, 500);
  } catch (error) {
    console.error('下载库存记录操作表失败:', error);
    ToastService.error('下载失败，请检查网络连接');
  } finally {
    isLoading.value = false;
  }
};
</script>

<style scoped>
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 1000;
}

.modal-content {
  background-color: white;
  padding: 24px;
  border-radius: 8px;
  width: 500px;
  max-width: 90%;
}

h3 {
  margin-top: 0;
  margin-bottom: 24px;
  color: #333;
  font-size: 18px;
}

.download-options {
  display: flex;
  flex-direction: column;
  gap: 16px;
  margin-bottom: 24px;
}

.download-option {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 16px;
  border: 1px solid #e0e0e0;
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.3s;
}

.download-option:hover {
  border-color: #1890ff;
  background-color: #f0f7ff;
  transform: translateY(-2px);
  box-shadow: 0 2px 8px rgba(24, 144, 255, 0.15);
}

.option-icon {
  width: 48px;
  height: 48px;
  border-radius: 50%;
  background-color: #f0f7ff;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #1890ff;
  font-size: 24px;
}

.option-info {
  flex: 1;
}

.option-info h4 {
  margin: 0 0 8px 0;
  font-size: 16px;
  color: #333;
}

.option-info p {
  margin: 0;
  font-size: 14px;
  color: #666;
}

.modal-actions {
  display: flex;
  justify-content: flex-end;
}

.cancel-btn {
  padding: 8px 16px;
  border: 1px solid #d9d9d9;
  background-color: white;
  border-radius: 4px;
  cursor: pointer;
  font-size: 14px;
  transition: all 0.3s;
}

.cancel-btn:hover {
  border-color: #d9d9d9;
  background-color: #f0f0f0;
}
</style>